/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.pf;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.pf.BaseChildGrantSalary;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pf.IBaseChildGrantSalaryService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.pf.BaseChildGrantSalaryVO;
import com.plian.system.wrapper.pf.BaseChildGrantSalaryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * 职工工资发放主表_子企业 控制器
 *
 * @author
 * @since 2019-09-27
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/basechildgrantsalary")
public class BaseChildGrantSalaryController extends BaseController {

	private IBaseChildGrantSalaryService baseChildGrantSalaryService;

	private FormWorkflowService formWorkflowService;

	private BaseChildGrantSalaryWrapper baseChildGrantSalaryWrapper;

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	public R<BaseChildGrantSalaryVO> detail(@RequestBody BaseChildGrantSalary baseGrantSalary) {
		BaseChildGrantSalary detail = baseChildGrantSalaryService.getOne(Condition.getQueryWrapper(baseGrantSalary));
		BaseChildGrantSalaryVO baseChildGrantSalaryVO = baseChildGrantSalaryWrapper.entityToVO(detail);
		return baseChildGrantSalaryService.getDetailObj(baseChildGrantSalaryVO);
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public ListResult<BaseChildGrantSalaryVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("filling_month");
		}
		QueryWrapper<BaseChildGrantSalary> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new BaseChildGrantSalary()), entityList
		);
		TokenSearch<BaseChildGrantSalary> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);

		IPage<BaseChildGrantSalary> pages = baseChildGrantSalaryService.page(Condition.getPage(query), queryWrapper);
		List<BaseChildGrantSalaryVO> baseChildGrantSalaryVOS = baseChildGrantSalaryWrapper.entityToVO(pages.getRecords());
		ListResult<BaseChildGrantSalaryVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(baseChildGrantSalaryVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@LogOperation("子企业职工工资发放-提交")
	public ObjResult submit(@Valid @RequestBody BaseChildGrantSalary baseChildGrantSalary) {
		String checkMessage = baseChildGrantSalaryService.checkMonth(baseChildGrantSalary);
		if (StringUtil.isNotBlank(checkMessage)){
			return new ObjResult(CustomCode.builder().message(checkMessage).code(1999).success(false).build());
		}

		boolean result = baseChildGrantSalaryService.submit(baseChildGrantSalary);
		HashMap<String, Object> processVariable = new HashMap<>();
		formWorkflowService.startProcessAndSimulate(String.valueOf(baseChildGrantSalary.getId()),baseChildGrantSalary.getFormType(),processVariable);

		return result ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@LogOperation("子企业职工工资发放-删除")
	public ObjResult remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return baseChildGrantSalaryService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
	}

	/**
	 * 子企业职工工资发放填报树 新增
	 * @param baseChildGrantSalary
	 * @return
	 */
	@PostMapping("/add/tree")
	@Resubmit(delaySeconds = 0)
	@LogOperation("子企业职工工资发放-填报树新增")
	public ObjResult addTree(@RequestBody BaseChildGrantSalary baseChildGrantSalary){
		return baseChildGrantSalaryService.addTree(baseChildGrantSalary);
	}

	/**
	 * 子企业职工工资发放填报树 判断当前登录账号是否有新增权限
	 * @return
	 */
	@GetMapping("/add/tree/isHave")
	public ObjResult addTree(){
		return baseChildGrantSalaryService.isHavePre();
	}

}
